#include "game_transam.h"

using namespace std;

//innitialise a car back to it's defaults
void Car::SetupCar()
{
	int carwidth,carheight;
	angle=0;acc=0;vel=0.0;decel=0.0;car=NULL;
	if(car) delete car;		//delete if already exists
	car=new Animation(GameLibrary,"player");	//get graphics for the car
	if(Configuration::GlobalErrorString!="")
		GameFramework->AbortSystem("animation 'player' could not be found for setupcar");

	//get size of car and the positions to show it
	car->GetStandardDimensions(&carwidth, &carheight);
	//car is always in the centre of the screen, so we already know
	//what bit of the map is being shown, so we get the centre of that
	//as it is shown on the screen
	screenx=((ShownMapWidth/2)+ScreenXOffset)-(carwidth/2);
	screeny=((ShownMapHeight/2)+ScreenYOffset)-(carheight/2);
	//what the car is doing
	Move=CAR_FREEWHEELING;
	Direction=CAR_STRAIGHT;
}

Car::~Car()
{
	if(car) delete car;
}

bool Car::NextMove()
{	
	//update the car. called every frame
	acc=0;
	decel=0.05;
	Move=CAR_FREEWHEELING;
	Direction=CAR_STRAIGHT;
	
	//key presses
	if(keypressed())
	{
		if(key[KEY_ESC])
		{
			clear_keybuf();
			return true;
		}
		//brake takes precedence
		if(key[KEY_DOWN]) Move=CAR_DECELERATE;
		else
		{
			if(key[KEY_UP]) Move=CAR_ACCELERATE;
		}
		if(key[KEY_RIGHT]) Direction=CAR_RIGHT;
		if(key[KEY_LEFT]) Direction=CAR_LEFT;
	}
	
	//handle movement
	if(Move==CAR_ACCELERATE)
		Accelerate();
	else if(Move==CAR_DECELERATE)
		Decelerate();

	//handle direction
	if(Direction==CAR_LEFT)
		TurnLeft();
	else if(Direction==CAR_RIGHT)
		TurnRight();

	//due to rounding problems of tiny fractions stop the car if we know it has stopped
	if(acc==0 && vel>=0) 
	{
		decel=0;
		vel=0;
	}
	
	//adjust speed
	vel=vel-(acc*0.1)+decel;

	//maximum or minimum velocity reached
	if(vel<-Car::MAXVEL) vel=-Car::MAXVEL;
	if(vel>Car::MAXVEL) vel=Car::MAXVEL;

	//check for edge bounces - there must be a better way!
	//left or top 
	if (mapx-(ShownMapWidth/2) < 0) 
	{
		if(angle>270)	//upleft
			angle=360-angle;
		else
			angle=180-(angle-180);
	}
	if (mapy-(ShownMapHeight/2) < 0) 
	{
		if(angle>270)	//upleft
			angle=360+180-angle;
		else
			angle=90+(90-angle);	//downleft
	}
	//right or bottom
	if (mapx+(ShownMapWidth/2) > mapblockwidth*mapwidth-16) 
	{
		if(angle<89)	//upleft
			angle=360-angle;
		else
			angle=180+(180-angle);	//downright
	}
	if (mapy+(ShownMapHeight/2) > mapblockheight*mapheight-1) 
	{
		if(angle<179)	//downright
			angle=180-angle;
		else
			angle=360-(angle-180);//downleft
	}


	//get movement for car
	xdistancemoved=sin((float)(angle*PI_Conv))*vel;
	ydistancemoved=cos((float)(angle*PI_Conv))*vel;

	//set map offset as being that of the player car
	this->mapx-=xdistancemoved;
	this->mapy+=ydistancemoved;
	return false;
}

void Car::Accelerate()
{
	vel-=0.1;
}

void Car::Decelerate()
{
	vel+=0.1;
}

void Car::TurnLeft()
{
	static int counter=1;

	counter++;
	if(counter>2)
	{
		angle-=9;
		if(angle<0) 
			angle=360+angle;
		counter=0;
	}
}

void Car::TurnRight()
{
	static int counter=1;

	counter++;
	if(counter>2)
	{
		angle+=9;
		if(angle>359) angle=360-angle;
		counter=0;
	}
	
}
